package yxy.game.pm2.active.module.timemail;

import org.joda.time.DateTime;
import org.redisson.api.RMap;
import yxy.apple.util.RandomStringUtility;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.module.BaseGameRedisLockBean;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.module.mail.MailTypeEnum;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class TimeMail extends BaseGameRedisLockBean {

	//bean ------------------------------------------------------------
	private String tmcode;		//playercode + shijianchuo
	private DateTime needsendtime;		//发送时间
	private String playercode;
	private boolean send;

	//mailbean
	private MailTypeEnum type;// 标题
	private String title;// 标题
	private String content;// 内容
	private ArrayList<Currency> rewards = new ArrayList<>();
	private CCReason reason;// 奖励原因
	
	public String getTmcode() {
		return tmcode;
	}
	public void setTmcode(String tmcode) {
		this.tmcode = tmcode;
	}
	public DateTime getNeedsendtime() {
		return needsendtime;
	}
	public void setNeedsendtime(DateTime needsendtime) {
		this.needsendtime = needsendtime;
	}
	public String getPlayercode() {
		return playercode;
	}
	public void setPlayercode(String playercode) {
		this.playercode = playercode;
	}
	public boolean isSend() {
		return send;
	}
	public void setSend(boolean send) {
		this.send = send;
	}
	public MailTypeEnum getType() {
		return type;
	}
	public void setType(MailTypeEnum type) {
		this.type = type;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public ArrayList<Currency> getRewards() {
		return rewards;
	}
	public void setRewards(ArrayList<Currency> rewards) {
		this.rewards = rewards;
	}
	public CCReason getReason() {
		return reason;
	}
	public void setReason(CCReason reason) {
		this.reason = reason;
	}

	//func ------------------------------------------------------------
	static public String key() {
		return String.format("TIME:MAIL");
	}

	static private RMap<String, TimeMail> touch() throws RedisException {
		return RedisManager.client(redis()).getMap(key());
	}

	static public Map<String, TimeMail> all() {
		try {
			Map<String, TimeMail> tms = touch().readAllMap();
			return tms;
		} catch (RedisException e) {
		}
		return new HashMap<>();
	}

	static public TimeMail get(String code) {
		try {
			TimeMail mail = touch().get(code);
			return mail;
		} catch (Exception e) {
			return null;
		}
	}

	public boolean set() {
		try {
			touch().put(getTmcode(), this);
			return true;
		} catch (RedisException e) {
			e.printStackTrace();
			return false;
		}
	}

	static public TimeMail create(DateTime needsendtime, String playerCode, MailTypeEnum type, String title, String content, CCReason reason, ArrayList<Currency> rewards) throws RedisException {
		TimeMail tm = new TimeMail();
		String tmcode = randomTaskCode(playerCode);
		RMap<String, TimeMail> tmmap = touch();
		while (tmmap.containsKey(tmcode)) {
			tmcode = randomTaskCode(playerCode);
		}
		
		tm.setTmcode(tmcode);
		tm.setNeedsendtime(needsendtime);
		tm.setSend(false);
		tm.setPlayercode(playerCode);
		tm.setType(type);
		tm.setTitle(title);
		tm.setContent(content);
		tm.setReason(reason);
		tm.setRewards(rewards);
		
		tm.set();
		return tm;
	}

	static private String randomTaskCode(String playercode) {
		String random = String.format("tt%s", RandomStringUtility.randomLowerNumeric(4));
		return playercode + DateTime.now().getMillis() + random;
	}
	
	public static void remove(String code) {
		try {
			touch().remove(code);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	public static void remove(Collection<String> codes) {
		try {
			RMap<String, TimeMail> mails = touch();
			for (String code : codes) {
				mails.remove(code);
			}
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}
	
	//lock ------------------------------------------------------------------------
	static private String lockKey() {
		return String.format("lock:%s", key());
	}
	
	static public boolean lockTimeMail() throws InterruptedException {
		return tryLock(lockKey());
	}

	static public void unlockTimeMail() {
		unlock(lockKey());
	}
	
}
